#ifndef _TCP_CLIENT_H
#define _TCP_CLIENT_H

#include<iostream>
#include<string>
#include<cstring>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>

#endif

class tcpClient{
    private:
      std::string _Ip;
      int _port;
      int _sock;
    public:
      tcpClient(std::string Ip="127.0.0.1",int port=8080)
        :_Ip(Ip)
        ,_port(port)
      {

      }
      void tcpClientInit()
      {
         _sock = socket(AF_INET, SOCK_STREAM, 0);
         if(_sock < 0)
         {
            std::cerr << "socket error "<<std::endl;
            exit(1);
         }
         struct sockaddr_in s;
         s.sin_family = AF_INET;
         s.sin_port = htons(_port);
         s.sin_addr.s_addr = inet_addr(_Ip.c_str());

         if( connect(_sock , (struct sockaddr*)&s ,sizeof(s) ) != 0 )
         { 
             std::cerr<<" connect error "<<std::endl;
             exit(2);
         }
      }
      void start()
      {
              char buffer[128];
              buffer[0] = 0;
              int res = read( 0 , buffer, sizeof(buffer)-1 );
              if( res < 0 )
              {
                  std::cerr << "read error"<<std::endl;
                  exit(3);
              }
              res--;//处理read的换行符
              buffer[res]= 0;

              send(_sock , buffer, res,0 );
              std::cout<<"##############"<<std::endl;
              buffer[0] = 0;
              res = recv(_sock, buffer , sizeof(buffer) -1 ,0);
              buffer[res] = '\0';

              std::cout<<buffer<<std::endl;
      }
      ~tcpClient()
      {
          close(_sock);
      }
};
